package com.ygo.feihua;
import android.os.*;
import android.support.v7.app.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AdapterView.*;
import cn.bmob.v3.*;
import cn.bmob.v3.b.*;
import cn.bmob.v3.datatype.*;
import cn.bmob.v3.exception.*;
import cn.bmob.v3.listener.*;
import com.ygo.feihua.*;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.adapter.*;
import com.ygo.feihua.util.*;
import java.io.*;
import java.util.*;

import android.view.View.OnClickListener;
import com.yuyh.library.imgsel.*;
import android.content.*;
import com.yuyh.library.imgsel.ui.*;

public class ShequAddTieType extends AppCompatActivity implements OnClickListener
{

	private ImageView sa_icon;
	private TextView sa_name;
	private Button sa_gl,sa_add;
	DialogUtils du;
	private Gongju gj;
	
	private TieType tieType;
	private String iconPath="";
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shequ_add_tietype);
		
		initView();
	}

	private void initView()
	{
		sa_icon=(ImageView) findViewById(R.id.sa_icon);
		sa_name=(TextView) findViewById(R.id.sa_name);
		sa_gl=(Button) findViewById(R.id.sa_gl);
		sa_add=(Button) findViewById(R.id.sa_add);
		
		du=DialogUtils.getdx(this);
		gj=Gongju.getdx(this);
		
		sa_icon.setOnClickListener(this);
		sa_gl.setOnClickListener(this);
		sa_add.setOnClickListener(this);
		// TODO: Implement this method
	}

	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.sa_icon:
				ISNav.getInstance().toListActivity(this, Gongju.getPicConfig(1), 1);
				
				break;
			case R.id.sa_add:
				if(iconPath.equals("")){
					gj.show("图标不能为空");
				}else{
				du.dialogj1(false,"","图标上传中,请稍等");
				final String name=sa_name.getText().toString().trim();
				
				final BmobFile bf=new BmobFile(new File(iconPath));
				bf.uploadblock(new UploadFileListener(){

						@Override
						public void done(BmobException p1)
						{
							if(p1==null){
								TieType tt=new TieType();
								tt.setName(name);
								tt.setIcon(bf);
								tt.setType(tieType);
								tt.save(new SaveListener<String>(){

										@Override
										public void done(String p1, BmobException p2)
										{
											du.dis();
											if(p2==null){
												gj.show("添加成功");
											}else{
												
												gj.toastBmobException(sa_add,"添加分类失败",p2);
											}
											// TODO: Implement this method
										}
									});
							}else{
								du.dis();
								gj.toastBmobException(sa_add,"图标上传失败",p1);
							}
							// TODO: Implement this method
						}
					});
				}
				
				break;
			case R.id.sa_gl:
				du.dialogj1(false,"","加载中,请稍等");
				BmobQuery<TieType> bq=new BmobQuery<TieType>();
				bq.order("createAt");
				bq.setLimit(50);
				bq.findObjects(new FindListener<TieType>(){

						@Override
						public void done(final List<TieType> ltt, BmobException p2)
						{
							if(p2==null){
						ListView ll=du.dialogl1(false,"",new TieTypeAdapter(ShequAddTieType.this,ltt));
							ll.setOnItemClickListener(new OnItemClickListener(){

									@Override
									public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
									{
										
										du.dis();
										tieType=ltt.get(p3);
										sa_gl.setText(tieType.getName());
										
										// TODO: Implement this method
									}
								});
								}
							// TODO: Implement this method
						}
					});
				break;
		}
		// TODO: Implement this method
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==1&&resultCode==RESULT_OK){
			ArrayList<String> photos=data.getStringArrayListExtra(ISListActivity.INTENT_RESULT);
			iconPath=photos.get(0);
			
		}
	}
	
	
	
	
}
